package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**  增强for循环/新循环  */
public class NewForDemo {
    public static void main(String[] args) {
        //数组循环遍历
        String[] array = {"one","two","three","four","five"};
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        //新循环
        for (String str:array){ //str表示的就是数组中的每个元素
            System.out.println(str);
        }

        //集合循环遍历
        Collection c = new ArrayList();//创建一个集合c
        c.add("one");//在集合c中添加一个元素one
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        Iterator it = c.iterator();//获取集合c的迭代器
        while (it.hasNext()){//当hasNext()为false时循环结束
            String str = (String)it.next();//获取下一个元素并强转为String类型
            System.out.println(str);//输出强转后的元素值
        }

        //新循环
        for (Object str1:c){
            System.out.println(str1);
        }



















    }
}
